package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyShopTree;
import cn.stylefeng.guns.sys.modular.system.entity.InventoryPrice;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyShopTreeMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.InventoryPriceMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.InventoryPriceParam;
import cn.stylefeng.guns.sys.modular.system.model.result.InventoryPriceResult;
import cn.stylefeng.guns.sys.modular.system.service.InventoryPriceService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-09-15
 */
@Service
public class InventoryPriceServiceImpl extends ServiceImpl<InventoryPriceMapper, InventoryPrice> implements InventoryPriceService {

    @Autowired
    private BtyShopTreeMapper btyShopTreeMapper;

    @Override
    public void add(InventoryPriceParam param) {
        InventoryPrice entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(InventoryPriceParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public ResponseData update(InventoryPriceParam inventoryPriceParam) {

        ResponseData data = ResponseData.success();
        //如果同一个店，添加过同店、同品类、同品项的数据，不允许添加
        InventoryPriceParam param = new InventoryPriceParam();
        BtyShopTree btyShopTree = btyShopTreeMapper.selectById(inventoryPriceParam.getShopTreeId());
        param.setShopCode(btyShopTree.getShopCode());
        param.setItemTypeCode(inventoryPriceParam.getItemTypeCode());
        param.setItemCode(inventoryPriceParam.getItemCode());
        List<InventoryPriceResult> inventoryPriceResults = this.baseMapper.selectInventoryPriceList(param);
        //修改时排除本身再判断
        if (inventoryPriceParam.getId() != null) {
            Iterator<InventoryPriceResult> iterator = inventoryPriceResults.iterator();
            while (iterator.hasNext()) {
                InventoryPriceResult price = iterator.next();
                if (price.getId() == inventoryPriceParam.getId()) {
                    iterator.remove();
                }
            }
        }

        //存在
        if (CollectionUtil.isNotEmpty(inventoryPriceResults)) {
            data.setSuccess(false);
            data.setMessage("数据保存失败，该店同品类品项数据已经存在");
        } else {
            InventoryPrice oldEntity = getOldEntity(inventoryPriceParam);
            InventoryPrice newEntity = getEntity(inventoryPriceParam);
            ToolUtil.copyProperties(newEntity, oldEntity);
            newEntity.setShopCode(btyShopTree.getShopCode());
            this.updateById(newEntity);
        }

        return data;
    }

    @Override
    public InventoryPriceResult findBySpec(InventoryPriceParam param) {
        return null;
    }

    @Override
    public List<InventoryPriceResult> findListBySpec(InventoryPriceParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(InventoryPriceParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public InventoryPrice selectOneById(Long id) {
        return this.baseMapper.selectOneById(id);
    }

    @Override
    public ResponseData addOne(InventoryPriceParam inventoryPriceParam) {

        ResponseData data = ResponseData.success();
        //如果同一个店，添加过同店、同品类、同品项的数据，不允许添加
        InventoryPriceParam param = new InventoryPriceParam();
        BtyShopTree btyShopTree = btyShopTreeMapper.selectById(inventoryPriceParam.getShopTreeId());
        param.setShopCode(btyShopTree.getShopCode());
        param.setItemTypeCode(inventoryPriceParam.getItemTypeCode());
        param.setItemCode(inventoryPriceParam.getItemCode());
        List<InventoryPriceResult> inventoryPriceResults = this.baseMapper.selectInventoryPriceList(param);

        //存在
        if (CollectionUtil.isNotEmpty(inventoryPriceResults)) {
            data.setSuccess(false);
            data.setMessage("数据保存失败，该店同品类品项数据已经存在");
        } else {
            InventoryPrice entity = getEntity(inventoryPriceParam);
            entity.setShopCode(btyShopTree.getShopCode());
            this.save(entity);
        }

        return data;
    }

    private Serializable getKey(InventoryPriceParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private InventoryPrice getOldEntity(InventoryPriceParam param) {
        return this.getById(getKey(param));
    }

    private InventoryPrice getEntity(InventoryPriceParam param) {
        InventoryPrice entity = new InventoryPrice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
